home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / netprog.zip / NETPROG.TAR / record / main4.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  2KB  |  65 lines

  1. /*
  2.  * Recording process, fourth try: use pseudo-terminals,
  3.  * copying the mode of the pty slave from the tty on stdin.
  4.  * We also set the mode of the tty on stdin to raw.
  5.  * Additionally, we now disassociate the child from its controlling
  6.  * terminal, so that the pty slave can become its control terminal.
  7.  */
  8.  
  9. #include    <sys/types.h>
  10. #include    <sys/ioctl.h>
  11. #include    <fcntl.h>
  12.  
  13. main(argc, argv, envp)
  14. int    argc;
  15. char    **argv;
  16. char    **envp;
  17. {
  18.     int    master_fd, slave_fd, childpid, fd;
  19.  
  20.     if (!isatty(0) || !isatty(1))
  21.         err_quit("stdin and stdout must be a terminal");
  22.  
  23.     master_fd = pty_master();
  24.     if (master_fd < 0)
  25.         err_sys("can't open master pty");
  26.     if (tty_getmode(0) < 0)
  27.         err_sys("can't get tty mode of standard input");
  28.  
  29.     if ( (childpid = fork()) < 0)
  30.         err_sys("can't fork");
  31.     else if (childpid == 0) {    /* child process */
  32.         /*
  33.          * First disassociate from control terminal.
  34.          */
  35.  
  36.         if ( (fd = open("/dev/tty", O_RDWR)) >= 0) {
  37.             if (ioctl(fd, TIOCNOTTY, (char *) 0) < 0)
  38.                 err_sys("ioctl TIOCNOTTY error");
  39.             close(fd);
  40.         }
  41.  
  42.         /*
  43.          * Now open slave pty device.
  44.          */
  45.  
  46.         if ( (slave_fd = pty_slave(master_fd)) < 0)
  47.             err_sys("can't open pty slave");
  48.         close(master_fd);
  49.         if (tty_setmode(slave_fd) < 0)
  50.             err_sys("can't set tty mode of pty slave");
  51.  
  52.         exec_shell(slave_fd, argv, envp);
  53.             /* NOTREACHED */
  54.     }
  55.  
  56.     if (tty_raw(0) < 0)            /* set stdin tty to raw mode */
  57.         err_sys("tty_raw error");
  58.  
  59.     pass_all(master_fd, childpid);
  60.  
  61.     if (tty_reset(0) < 0)            /* reset stdin mode */
  62.         err_sys("tty_reset error");
  63.     exit(0);
  64. }
  65.